package com.accp.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.struts2.ServletActionContext;
import com.accp.biz.EmailBiz;
import com.accp.pojo.Email;
import com.accp.pojo.Users;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class EmailAction extends ActionSupport {
	private Email email;
	
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	
	public Email getEmail() {
		return email;
	}
	public void setEmail(Email email) {
		this.email = email;
	}
	public File getUpload() {
		return new File(email.getUpload());
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	private EmailBiz emailBiz;
	
	public void setEmailBiz(EmailBiz emailBiz) {
		this.emailBiz = emailBiz;
	}
	/**
	 * 查询收件人
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String recipients(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		List<String> list = emailBiz.recipientsList();
		if(null!=list){
			request.put("recipientsList", list);
			return "recipients";
		}
		return "error";		
	}
	/**
	 * 添加（发送）邮件
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String add(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		recipients();//查询收件人
		Date date = new Date();
		email.setTime(date);
		if(null!=upload){
			upload();//上传文件
			email.setUpload("/upload/"+uploadFileName);
		}
		if(emailBiz.add(email)){
			request.put("message", "发送成功");
			return "add";
		}
		return "error";
	}
	/**
	 * 查收邮件
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String box(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		Users users = (Users) session.get("user");
		String userName = users.getUserName();
		List<Email> list = emailBiz.findByUserName(userName);
		if(null!=list){
			request.put("emails", list);
			return "box";
		}
		return "error";
	}
	/**
	 * 删除邮件到垃圾箱
	 * @return
	 */
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		Integer isDeleted = Integer.parseInt(request.getParameter("isDeleted").trim());
		Email email = emailBiz.findById(id);
		email.setIsDeleted(isDeleted);
		if(emailBiz.delete(email)){
			return "delete";
		}
		return "error";
	}
	/**
	 * 彻底删除邮件
	 * @return
	 */
	public String deleteReal(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		Integer isDeleted = Integer.parseInt(request.getParameter("isDeleted").trim());
		Email email = emailBiz.findById(id);
		email.setIsDeleted(isDeleted);
		if(emailBiz.deleteReal(email)){
			return "deleteReal";
		}
		return "error";
	}
	/**
	 * 还原邮件
	 * @return
	 */
	public String reduction(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Integer id = Integer.parseInt(request.getParameter("id"));
		Email email = emailBiz.findById(id);
		email.setIsDeleted(1);
		if(emailBiz.updateIsDeleted(email)){
			return "reduction";
		}
		return "error";
	}
	/**
	 * 查看垃圾邮件
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String spam(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		Users users = (Users) session.get("user");
		String userName = users.getUserName();
		List<Email> list = emailBiz.spamList(userName);
		if(null!=list){
			request.put("spamList", list);
			return "spam";
		}
		return "error";
	}
	/**
	 * 查看邮件内容并修改读取状态
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String find(){
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		int id = Integer.parseInt(ServletActionContext.getRequest().getParameter("id"));
		Email email = emailBiz.findById(id);
		if(null!=email){
			if(email.getIsRead()==0){
				email.setIsRead(1);
				emailBiz.updateIsRead(email);//修改邮件读取状态
			}
			
			if(null!=email.getUpload()){//获取附件名
				String fileName = email.getUpload().substring(email.getUpload().lastIndexOf("/")+1);
				request.put("fileName", fileName);
			}
			request.put("email", email);
			return "find";
		}
		return "error";
	}
	
	
	/**
	 * 上传文件
	 */
	public String upload() {
		String savePath = ServletActionContext.getServletContext()
					.getRealPath("/upload/"+this.uploadFileName);
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			if(new FileInputStream(upload).available() > 1024 * 1024 * 9 ){
				HttpServletRequest request = ServletActionContext.getRequest();
				request.setAttribute("alert", "文件不能大于9M,请重新选择");
				return INPUT;
			}
			fis = new FileInputStream(upload);
			fos = new FileOutputStream(savePath);
			IOUtils.copy(fis, fos);
			fos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(null!=fos || null!=fis){
				try {
					fos.close();
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return SUCCESS;
	}
		
	/**
	 * 分页查看用户信息
	 * 
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String selectFirstPage(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
		try{
			Users users = (Users) session.get("user");
			String userName = users.getUserName();
			List list = emailBiz.selectByPage("Email", 1, 15,userName);
			int totalPage = emailBiz.searchRecordsNextPage(15, "Email",userName);
			request.put("list", list);
			request.put("totalPage", totalPage);
			request.put("currentPage", 1);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "selectFirstPage";
	}
	/**
	 * 分页查找下一页
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public String selectNextPage(){
		Map<String,Object> session = ActionContext.getContext().getSession();
		HttpServletRequest request = ServletActionContext.getRequest();
		int next = Integer.parseInt(request.getParameter("page"));
		try{
			Users users = (Users) session.get("user");
			String userName = users.getUserName();
			List list = emailBiz.selectByPage("Email", next, 15,userName);
			int total = emailBiz.searchRecordsNextPage(15, "Email",userName);
			request.setAttribute("list", list);
			request.setAttribute("currentPage", next);
			request.setAttribute("totalPage", total);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "selectNextPage";
	}
}
